package com.cqjtu.eecs.controller;


import com.cqjtu.eecs.entity.vo.GraduateRequirementTrainingGoalTemplateVo;
import com.cqjtu.eecs.service.IGraduateRequirementTrainingGoalTemplateService;
import com.cqjtu.eecs.utils.Result;
import com.cqjtu.eecs.utils.ResultSupportMatrix;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 重交岳云鹏
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/graduate-requirement-training-goal-template")
@Api(tags = "毕业要求&培养目标弱实体板块")
public class GraduateRequirementTrainingGoalTemplateController {
    @Autowired
    private IGraduateRequirementTrainingGoalTemplateService iGraduateRequirementTrainingGoalTemplateService;
    @Autowired
    private ObjectMapper objectMapper;

    @ApiOperation("根据专业代码得到毕业要求对培养目标的支撑矩阵")
    @GetMapping("/supportMatrixByMajorCode/{majorCode}")
    public String supportMatrix(@PathVariable("majorCode") String majorCode)  throws JsonProcessingException {
        List<GraduateRequirementTrainingGoalTemplateVo> datas = iGraduateRequirementTrainingGoalTemplateService.selectSupportMatrixByMajorCode(majorCode);
        //找最大的培养目标序号
        int maxtrainingGoalCode=-1;
        Collections.sort(datas,(o1,o2)->Integer.compare(o2.getTrainingGoalCode(),o1.getTrainingGoalCode()));
        maxtrainingGoalCode=datas.get(0).getTrainingGoalCode();
        Collections.sort(datas,(o1,o2)->Integer.compare(o1.getTrainingGoalCode(),o2.getTrainingGoalCode()));
        Set<Integer> set=new HashSet<>();
        for (int i=0;i<datas.size();i++){
            set.add(datas.get(i).getGraduateRequirementCode());
        }
        List<List<Integer>> ans=new ArrayList<>();
        for (int i=0;i<datas.size();i++){
            List<Integer> temp=new ArrayList<>();
            for (int j=0;j<datas.size();j++){
                if(datas.get(j).getGraduateRequirementCode()==i+1){
                    temp.add(datas.get(j).getTrainingGoalCode());
                }
            }
            if(i==set.size()) break;
            ans.add(temp);
        }
        ResultSupportMatrix<List<List<Integer>>> end=new ResultSupportMatrix<>(maxtrainingGoalCode,ans);
        return objectMapper.writeValueAsString(Result.success(end));
    }
}
